package com.wanglianyun.aspect.parse;

import com.wanglianyun.aspect.Replace;

import java.lang.reflect.Field;

/**
 * Description:
 * Copyright: Copyright (c) 2024
 * Create Time: 2024-11-14 21:45
 *
 * @author wanglianyun
 */
public class ReplaceParse {

    public Object parse(Object o) {
        Class<?> myclass = null;
        Object o1 = o;
        try {
            myclass = o.getClass();
            Field[] declaredFields = myclass.getDeclaredFields();
            for (Field field : declaredFields) {
                if (field.isAnnotationPresent(Replace.class)) {
                    field.setAccessible(true);
                    Replace replace = field.getAnnotation(Replace.class);
                    String source = replace.source();
                    String target = replace.target();
                    String s = String.valueOf(field.get(o1));
                    String replaceStr = s.replace(target, source);
                    field.set(o1, replaceStr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o1;
    }
}
